package com.heima.pojo.dto;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.heima.pojo.entity.User;
import lombok.Data;

import java.io.IOException;

@Data
@TableName("tb_user")
public class UserDto {
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    private Integer roleId;
    private String userName;
    private String loginName;
    private String password;
    private String regionId;
    private String regionName;
    private String secret;
    private String roleCode;
    private String mobile;
    private String image;
    private Boolean status; // 使用 Boolean 类型接收

    // 转换为 User 实体的方法
    public User toUser() {
        User user = new User();
        user.setRoleId(this.roleId);
        user.setUserName(this.userName);
        user.setLoginName(this.loginName);
        user.setPassword(this.password);
        user.setRegionId(this.regionId);
        user.setRegionName(this.regionName);
        user.setSecret(this.secret);
        user.setRoleCode(this.roleCode);
        user.setMobile(this.mobile);
        user.setImage(this.image);
        // 安全地将 Boolean 转换为 Integer
        if (this.status != null) {
            user.setStatus(this.status ? 1 : 0);
        } else {
            user.setStatus(null);
        }
        return user;
    }

}
